<?php

if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

/**
 * 头部模块
 */
class Header extends MY_Controller
{

    //默认加载的CSS
    private $_default_css;
    //默认加载的js
    private $_default_js;

    function __construct()
    {
        parent::__construct();
        $this->_default_css = array(
            'theme/' . config_item( 'theme' ) . '/style.css',
        );
        $this->_default_js = array(
            'js/jquery-1.7.min.js',
        );
    }

    /**
     * 头部模块
     * @param string 页面标题
     * @param array $extra_css 额外加载CSS文件
     * @param array $extra_js 额外加载JS文件
     */
    public function index( $title = '', $extra_css = array( ), $extra_js = array( ) )
    {
        $data = array(
            'title' => empty( $title ) ? config_item( 'appname' ) : $title . ' &raquo; ' . config_item( 'appname' ),
            'description' => '',
            'keywords' => '',
            'css' => $this->_css( $extra_css ),
            'js' => $this->_js( $extra_js ),
            'appname' => config_item( 'appname' ),
            'username' => $this->session->getUserSession( 'username' ),
            'groupname' => $this->session->getUserSession( 'groupname' ),
            'greeting' => $this->_greeting(),
            'validUser' => $this->session->validUser(),
            'navigation' => $this->navigation(),
        );
        $this->load->view( 'header', $data );
    }

    /**
     * 导航栏
     * @return array
     */
    public function navigation()
    {
        $cur = $this->uri->segment(1);
        return array(
            'rent' => array(
                'title' => '租房',
                'url' => my_url( 'rent', '', '', false ),
                'active' => ($cur == '' || $cur == 'rent'),
            ),
            'sell' => array(
                'title' => '买房',
                'url' => my_url( 'sell', '', '', false ),
                'active' => ($cur == 'sell'),
            ),
        );
    }

    /**
     * 整理css
     * @return string
     */
    private function _css( $extra_css = array( ) )
    {
        $css_arr = array_unique( array_merge( $this->_default_css, $extra_css ) );
        $result = '';
        foreach ( $css_arr as $single )
            $result .= load_css( $single );
        return $result;
    }

    /**
     * 整理js
     * @return string
     */
    private function _js( $extra_js = array( ) )
    {
        $js_arr = array_unique( array_merge( $this->_default_js, $extra_js ) );
        $result = '';
        foreach ( $js_arr as $single )
            $result .= load_js( $single );
        return $result;
    }

    /**
     * 根据当前时间获取问候语
     * @return string
     */
    private function _greeting()
    {
        $hour = intval( date( 'H' ) );
        if ( $hour >= 0 && $hour < 9 )
        {
            return '早上好';
        }
        else if ( $hour >= 9 && $hour < 12 )
        {
            return '上午好';
        }
        else if ( $hour >= 12 && $hour < 18 )
        {
            return '下午好';
        }
        else
        {
            return '晚上好';
        }
    }

}